﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.Utilities;
using FrameworkManapro.WebUI;

namespace GMAC.ReportingSystem.Business.Components
{
    public class CheckComponent : IBusinessComponent<Entities.Check>
    {
        private DataAccess.CheckDataAccess dac;

        public CheckComponent()
        {
            dac = new DataAccess.CheckDataAccess();
        }

        #region IBusinessComponent<Check> Members

        public int Create(Entities.Check entityToCreate)
        {
            if (Validate(entityToCreate))
            {
                var result = dac.Insert(entityToCreate);
                return result;
            }
            return 0;
        }

        public int Delete(Entities.Check entityToDelete)
        {
            if (entityToDelete.ID <= 0)
                throw new ArgumentOutOfRangeException("CheckID", "Value must be greater than zero.");

            return dac.Delete(entityToDelete);
        }

        public int Edit(Entities.Check entityToEdit)
        {
            if (entityToEdit.ID <= 0)
                throw new ArgumentOutOfRangeException("CheckID", "Value must be greater than zero.");

            return Validate(entityToEdit) ? dac.Update(entityToEdit) : 0;
        }

        public Entities.Check GetDetails(object entityId)
        {
            return dac.Get(entityId);
        }

        protected bool Validate(Entities.Check Check)
        {
            if (Check == null) throw new ArgumentNullException("Check", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Check.ID.ToString())) throw new ArgumentNullException("ID", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Check.dDate.ToString())) throw new ArgumentNullException("dDate", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Check.CheckNumber.ToString())) throw new ArgumentNullException("CheckNumber", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Check.Bank.DebitBankCode.ToString())) throw new ArgumentNullException("BankId", "The argument canno be null.");
            if (string.IsNullOrEmpty(Check.Amount.ToString())) throw new ArgumentNullException("Amount", "The argument canno be null.");
            

            return true;
        }

        #endregion


        public Entities.CheckCollection GetList()
        {
            return dac.GetList();
        }

        public Entities.CheckCollection Search(string CheckNumber, DateTime? dDate, int? BankId, PagingDetails paging)
        {
            if (CheckNumber == null) CheckNumber = string.Empty;
            return dac.Search( CheckNumber, dDate, BankId, paging);
        }



    }
}
